<?php

/**
 * 前台访问控制器类
 * 
 * @package    01CMS
 * @subpackage www
 * @author     rolong at vip.qq.com
 * @version    1.0.0
 * @link       http://www.01cms.com
 */

class MemberController extends Controller
{
    public $Member;
    public $templateDir;

    function __construct ()
    {
        parent::__construct();
        $this->templateDir = ROOT_PATH . '/template/member';
        $this->Member = model('Member');
        if (! in_array($this->m, array('login', 'register')))
        {
            if (! $this->Member->isLogined())
            {
                redirect('member/login');
            }
        }
    }

    function login ($action = '')
    {
        if ($action == 'check')
        {
            $data = $this->Load->post('validateCode|验证码不能为空,username|用户名不能为空,password|密码不能为空');
            
            if ($error = $this->Load->getError())
            {
                response($error);
            }
            
            if (! Session::checkCode('validateCode', $data['validateCode']))
            {
                response('验证码不正确');
            }
            
            if ($this->Member->login($data))
            {
                response('登陆成功', TRUE);
            }
            else
            {
                response($this->Member->getError());
            
            }
        }
        else
        {
            $this->Load->view('smarty');
        }
    }

    function register ($action = '')
    {
        if ($action == 'check')
        {
            $data = $this->Load->post('validateCode|验证码不能为空,username|用户名不能为空,password|密码不能为空,email|邮箱不能为空,groupId');
            
            if ($error = $this->Load->getError())
            {
                response($error);
            }
            
            if (! Session::checkCode('validateCode', $data['validateCode']))
            {
                response('验证码不正确');
            }
            
            if (! Filter::isName($data['username']))
            {
                response('用户名不合法');
            }
            
            if (! Filter::isPwd($data['password']))
            {
                response('密码不合法');
            }
            
            if (! Filter::isEmail($data['email']))
            {
                response('邮箱不正确');
            }
            
            unset($data['validateCode']);
            
            if ($this->Member->addUser($data))
            {
                response('注册成功', TRUE);
            }
            else
            {
                response($this->Member->getError());
            }
        }
        else
        {
            $this->Load->view('smarty');
        }
    }

    function logout ()
    {
        $this->Member->logout();
        redirect('member/login');
    }

    function index ()
    {
        $this->Load->view('smarty');
    }
}